﻿using System;
using System.Collections.Generic;
using System.Linq;
using Diploma.Models.IRepositories;

namespace Diploma.Models.Repositories
{
    internal class StudentsGroupsRepository : IStudentsGroupsRepository
    {
        private readonly DiplomaEntities dataContext;

        public StudentsGroupsRepository(DiplomaEntities dataContext)
        {
            this.dataContext = dataContext;
        }

        public StudentsGroup GetStudentsGroup(Guid id)
        {
            return
                dataContext.StudentsGroups.Where(g => g.StudentsGroupId == id && g.DeletionDate == null).FirstOrDefault
                    ();
        }

        public void Edit(StudentsGroup newStudentsGroup)
        {
            StudentsGroup oldtudentsGroup = GetStudentsGroup(newStudentsGroup.StudentsGroupId);
            if (oldtudentsGroup == null)
                throw new Exception();

            oldtudentsGroup.Name = newStudentsGroup.Name;
            oldtudentsGroup.QualificationLevelId = newStudentsGroup.QualificationLevelId;

            dataContext.SaveChanges();
        }

        public void Add(StudentsGroup group)
        {
            group.StudentsGroupId = Guid.NewGuid();
            dataContext.AddToStudentsGroups(group);
            dataContext.SaveChanges();
        }

        public void Delete(StudentsGroup group)
        {
            group.MarkForDeletion();
            //_dataContext.DeleteObject(group);
            dataContext.SaveChanges();
        }

        public IEnumerable<StudentsGroup> GetAllGroups()
        {
            return dataContext.StudentsGroups.Where(sg => sg.DeletionDate == null).OrderBy(g => g.Name);
        }

        public IEnumerable<StudentsGroup> GetDeletedStudentsGroups()
        {
            return dataContext.StudentsGroups.Where(s => s.DeletionDate != null).OrderBy(g => g.Name);
        }

        public void Restore(Guid groupId)
        {
            StudentsGroup deletedgroup = GetDeletedStudentsGroups().FirstOrDefault(g => g.StudentsGroupId == groupId);
            if (deletedgroup == null)
                throw new Exception("Group not found");
            deletedgroup.UnmarkFromDeletion();
            dataContext.SaveChanges();
        }

        public Dictionary<QualificationLevel, IEnumerable<StudentsGroup>> GetDeletedGroupsGroupesByLevel()
        {
            IEnumerable<StudentsGroup> allDeleted = GetDeletedStudentsGroups();

            IEnumerable<IGrouping<QualificationLevel, StudentsGroup>> grouped = from dg in allDeleted
                                                                                group dg by dg.QualificationLevel
                                                                                into g
                                                                                select g;
            Dictionary<QualificationLevel, IEnumerable<StudentsGroup>> v = grouped.ToDictionary(g => g.Key,
                                                                                                g =>
                                                                                                (
                                                                                                IEnumerable
                                                                                                    <StudentsGroup>)
                                                                                                g.OrderBy(sg => sg.Name));
            return v;
        }
    }
}